package controllers

import (
	"encoding/json"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/cache"
	_ "github.com/astaxie/beego/cache/redis"
	"time"
	"yunshangxiaozhao/utils"
)

const (
	SignName                 = "云上校招"
	accessKeyId              = "LTAI4Fn1AnpeKLucEfh247jA"
	accessSecretId           = "T0kYwcHBCvxTy6ADzRYa6D5sO8viuu"
	RegisterTemplateCode     = "SMS_177185119"
	ChangePhoneTemplateCode  = "SMS_177185117"
	LoginConfirmTemplateCode = "SMS_177185121"
)

var (
	redis, _  = cache.NewCache("redis", `{"key":"sms","conn":"121.199.1.204:6379","dbNum":"0"}`)
	client, _ = dysmsapi.NewClientWithAccessKey(
		"cn-hangzhou",
		accessKeyId,
		accessSecretId)
)

// Operations about SMS
type SMSController struct {
	beego.Controller
}

func (s *SMSController) RetData(resp map[string]interface{}) {
	resp["code"] = 200
	s.Data["json"] = resp
	s.ServeJSON()
}

// @Title SendRegisterSMS
// @Description send sms message for register
// @Param telephone	body	string	true	"body for user content"
// @Success 200 {string} send verify message success
// @Failure 400 body is empty
// @router /register/send [post]
func (s *SMSController) SendRegisterSMS() {
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	_ = json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	tel := req["telephone"].(string)
	code := utils.GenerateSMSCode()

	s.SetSession("tel", tel)
	err := redis.Put(tel, code, 2*60*time.Second)
	if err != nil {
		s.Abort("SendSMSFail")
	}

	request := dysmsapi.CreateSendSmsRequest()
	request.Scheme = "https"

	request.PhoneNumbers = tel
	request.SignName = SignName
	request.TemplateCode = RegisterTemplateCode
	request.TemplateParam = "{code: " + code + "}"

	_, err = client.SendSms(request)
	if err != nil {
		s.Abort("SendSMSFail")
	}

	resp["msg"] = "发送验证短信成功"
	s.RetData(resp)
}

// @Title SendPhoneChangeSMS
// @Description send sms message for phone change
// @Param	telephone	body	string	true	"phone number to change"
// @Success 200 {string} send verify message success
// @Failure 400 body is empty
// @router /tel/send [post]
func (s *SMSController) SendPhoneChangeSMS() {
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	if err != nil {
		s.Abort("JsonToObject")
	}
	tel := req["telephone"].(string)
	code := utils.GenerateSMSCode()

	s.SetSession("tel", tel)
	err = redis.Put(tel, code, 2*60*time.Second)
	if err != nil {
		s.Abort("SendSMSFail")
	}

	request := dysmsapi.CreateSendSmsRequest()
	request.Scheme = "https"

	request.PhoneNumbers = tel
	request.SignName = SignName
	request.TemplateCode = ChangePhoneTemplateCode
	request.TemplateParam = "{code: " + code + "}"

	_, err = client.SendSms(request)
	if err != nil {
		s.Abort("SendSMSFail")
	}

	resp["msg"] = "发送更换手机号短信成功"
	s.RetData(resp)
}

// @Title VerifySMS
// @Description verify sms message
// @Param body	body	string	true	"body for user content"
// @Success 200 {int} models.User.Id
// @Failure 400 body is empty
// @router /verify [post]
func (s *SMSController) VerifySMS() {
	resp := make(map[string]interface{})
	req := make(map[string]interface{})
	_ = json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	code := req["code"].(string)
	tel := s.GetSession("tel").(string)
	passCode := string(redis.Get(tel).([]byte))

	if code != passCode {
		s.Abort("VerifyCode")
	}

	resp["msg"] = "验证成功"
	s.RetData(resp)
}
